/*
 * Galaxium Messenger
 * 
 * Copyright (C) 2007 Philippe Durand <draekz@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.Collections.Generic;

using Gtk;

using Anculus.Core;
using Anculus.Gui;

using Galaxium.Core;
using Galaxium.Protocol;
using Galaxium.Protocol.Gui;
using Galaxium.Gui;
using Galaxium.Gui.GtkGui;

namespace Galaxium.Protocol.Facebook.GtkGui
{
	public class FBAccountWidget : BasicAccountWidget
	{
		//Entry _entryPID;
		
		public FBAccountWidget ()
			: base (FBProtocol.Instance)
		{
			
		}
		
		public FBAccountWidget (IProtocol protocol)
			: base (protocol)
		{
			
		}
		
		public override void Initialize ()
		{
			OmitPresenceCombo = true;
			
			//_entryPID = new Entry ();
			//_entryPID.KeyPressEvent += EntryPIDKeyPress;
			//_entryPID.Text = "0";
			
			//AddCustomAccountItem ("Profile ID:", _entryPID);
			
			base.Initialize ();
			
			(_layout_widget as Label).Text = "Email:"; //TODO: This is a horrid hack
			
			_accountCombo.TooltipText = "Provide the email address associated with your facebook account.";
		}
		
		void EntryPIDKeyPress (object sender, KeyPressEventArgs args)
		{
			if (!IsKeyNumeric (args.Event.Key))
				args.RetVal = true;
		}
		
		bool IsKeyNumeric (Gdk.Key k)
		{
			return (k == Gdk.Key.Key_0) || (k == Gdk.Key.Key_1) ||
					(k == Gdk.Key.Key_2) || (k == Gdk.Key.Key_3) ||
					(k == Gdk.Key.Key_4) || (k == Gdk.Key.Key_5) ||
					(k == Gdk.Key.Key_6) || (k == Gdk.Key.Key_7) ||
					(k == Gdk.Key.Key_8) || (k == Gdk.Key.Key_9) ||
					(k == Gdk.Key.KP_0) || (k == Gdk.Key.KP_1) ||
					(k == Gdk.Key.KP_2) || (k == Gdk.Key.KP_3) ||
					(k == Gdk.Key.KP_4) || (k == Gdk.Key.KP_5) ||
					(k == Gdk.Key.KP_6) || (k == Gdk.Key.KP_7) ||
					(k == Gdk.Key.KP_8) || (k == Gdk.Key.KP_9);
		}
		
		protected override void PopulateFields ()
		{
			base.PopulateFields ();
		}

		protected override IAccount SetAccount ()
		{
			FBAccount account = GetAccount(_accountCombo.Entry.Text) as FBAccount;
			
			if (account != null)
			{
				//account.ProfileID = long.Parse (_entryPID.Text);
				account.Password = _passwordEntry.Text;
				account.AutoConnect = _autoconnectCheck.Active;
				account.RememberPassword = _rememberPassCheck.Active;
				account.InitialPresence = FBPresence.Online;
				return account;
			}
			else
			{
				FBAccount newAccount = new FBAccount (_accountCombo.Entry.Text, _passwordEntry.Text, _autoconnectCheck.Active, _rememberPassCheck.Active);
				
				AccountUtility.AddAccount (newAccount);
				
				return newAccount;
			}
		}
		
		protected override void LoadAccountInfo ()
		{
			base.LoadAccountInfo ();
			
			// Here we would load all the FB specific settings into the UI.
			
			FBAccount account = Account as FBAccount;
			
			if (account != null)
			{
				
			}
		}
		
		public override void EnableFields ()
		{
			base.EnableFields ();
			
			// Also enable custom protocol widgets.
		}
		
		public override void DisableFields (bool omit_cancel)
		{
			base.DisableFields (omit_cancel);
			
			// Also disable custom protocol widgets.
		}
		
		protected override Gdk.Pixbuf PresenceImageLookup (IPresence item)
		{
			return IconUtility.GetIcon (FBPresence.GetIconString (item), IconSizes.Small);
		}
		
		protected override void ConnectButtonClicked (object sender, EventArgs args)
		{
			// When connect is clicked, we create a session and then we send it off to the basic handler
			
			_current_session = CreateSession () as FBSession;
			
			if (_current_session == null)
				return;
			
			base.ConnectButtonClicked (sender, args);
		}
		
		protected override void SettingsButtonClicked (object sender, EventArgs args)
		{
		}
	}
}